var _inherit = require('core/inherit.js');
var _Container = require('component/container.js');

var _Utility = require('scenes/utility.js');
var R = require('res/texture.js').R;
var S = require('res/style.js');

var TOTAL_SKIN = 12;

// ==========================
// CellCommon
// ==========================
function CellCommon(_global) {
    _Container.call(this);

    this.mBgs = new Array(6);
    this.addChild(this.mBgs[0] = _global.spriteNode(R.cards.card_1));
    this.addChild(this.mBgs[1] = _global.spriteNodeGray(R.cards.card_1));
    this.addChild(this.mBgs[2] = _global.spriteNode(R.cards.card_2));
    this.addChild(this.mBgs[3] = _global.spriteNodeGray(R.cards.card_2));
    this.addChild(this.mBgs[4] = _global.spriteNode(R.cards.card_3));
    this.mBgs[5] = this.mBgs[4];

    this.mSkins = new Array(TOTAL_SKIN);
    var start = R.skin.skin_01;
    for (var i = 0; i < TOTAL_SKIN; i++) {
        this.addChild(this.mSkins[i * 2] = _global.spriteNode(start + i));
        this.addChild(this.mSkins[i * 2 + 1] = _global.spriteNodeGray(start + i));
    }

    // creat all cell count texts
    this.mText12 = [];
    this.appendText(this.mText12, S.game.cells.count_12, 1);
    this.appendText(this.mText12, S.game.cells.count_12, 2);
    this.mText = [];
    this.mTextTop = [];
    var start = 3;
    while (start <= _Utility.MAX_COUNT) {
        this.appendText(this.mText, S.game.cells.count_3, start);
        this.appendText(this.mTextTop, S.game.cells.count_top, start);
        start *= 2;
    }

    for (var i = 0, cs = this.mChildren, l = cs.length; i < l; i++) {
        cs[i].setAnchor(0.5, 0.5);
    }
}
_inherit(CellCommon, _Container);
CellCommon.prototype.appendText = function (arr, style, score) {
    var text = style.createText(score);
    text.setAnchor(0.5, 0.5);
    if (score > 768) {
        text.setScale(0.75, 0.75);
    } else if (score > 96) {
        text.setScale(0.9, 0.9);
    } else {
        text.setScale(1, 1);
    }
    arr.push(text);
    this.addChild(text);
};
CellCommon.prototype.drawRefer = function (context, refer) {
    this.mBgs[refer.mBg + refer.mGray].draw(context);
};
CellCommon.prototype.drawText = function (index, istop, context) {
    if (istop) {
        this.mTextTop[index].draw(context);
    } else if (index < 0) {
        this.mText12[-index - 1].draw(context);
    } else {
        this.mText[index].draw(context);
    }
};

// ==========================
// TextRefer
// ==========================
function TextRefer(common) {
    _Container.call(this);
    this.mCommon = common;
    this.mIndex = 0;
    this.mTop = false;
}
_inherit(TextRefer, _Container);
TextRefer.prototype.drawContent = function (context) {
    this.mCommon.drawText(this.mRefer, this.mTop, context);
};
TextRefer.prototype.updateText = function (count, istop) {
    if (count < 3) {
        this.mTop = false;
        this.mRefer = -count;
    } else {
        this.mTop = istop;
        this.mRefer = _Utility.getScoreOrder(count);
    }
};

// ==========================
// Refer
// ==========================
function Refer(common) {
    _Container.call(this);
    this.mCommon = common;

    this.mBg = 0;
    this.mSkin = -1;
    this.mGray = 0;
}
_inherit(Refer, _Container);
Refer.prototype.setGray = function (gray) {
    this.mGray = gray ? 1 : 0;
};
Refer.prototype.updateSkin = function (count) {
    // bgs
    if (count < 3) {
        this.mSkin = -1;
        if (count == 1) {
            this.mBg = 0;
        } else {
            this.mBg = 2;
        }

        return this;
    }
    this.mBg = 4;

    // skins
    var start = 0;
    var wanted = 3;
    while (start < TOTAL_SKIN) {
        if (count <= wanted) {
            break;
        }
        wanted *= 2;
        start++;
    }
    if (start == TOTAL_SKIN) {
        start = TOTAL_SKIN - 1;
    }
    this.mSkin = start * 2;

    return this;
};
Refer.prototype.drawContent = function (context) {
    this.mCommon.drawRefer(context, this);
};

// ==========================
// CellCommon
// ==========================
var mCommon;// single instance

exports.recoverFromLostContext = function (context) {
    mCommon.recoverFromLostContext(context);
};
exports.load = function (global) {
    mCommon = new CellCommon(global);
};
exports.createSkin = function (count) {
    return new Refer(mCommon).updateSkin(count);
};
exports.createText = function (count) {
    var text = new TextRefer(mCommon);
    text.updateText(count);
    return text;
};
